<template>

<!-- 抽离封装关注用户组件 -->
 <van-button
            class="follow-btn"
            type="info"
            color="#3296fa"
            round
            size="small"
            icon="plus"
            v-if="!isFollowed"
            @click="onFollowed"
            :loading="isFollowLoading"
          >关注</van-button>
          <van-button
            class="follow-btn"
            round
            size="small"
            v-else
            @click="onFollowed"
            :loading="isFollowLoading"
          >已关注</van-button>

</template>

<script>
import { deleteUser, followUser } from '@/api/user.js'
export default {
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      required: true,
      type: [String, Object, Number]
    }

  },
  data () {
    return {
      isFollowLoading: false
    }
  },
  model: {
    prop: 'isFollowed',
    event: 'updateFollowed'
  },
  created () {
    // return {
    //   isFollowLoading: false
    // }
  },

  methods: {
    // 点击关注或已关注
    async  onFollowed () {
      this.isFollowLoading = true

      try {
        if (this.isFollowed) {
          console.log('取消关注')
          await deleteUser(this.autId)
        } else {
          console.log('关注')
          await followUser(this.autId)
        }
        // 请求成功了
        this.$emit('updateFollowed', !this.isFollowed)
        this.isFollowLoading = false
      } catch (err) {

      }
    }

  }
}
</script>

<style scoped lang='less'>

</style>
